import request from '@/utils/request'
import qs from 'qs'
import { headers } from '@/utils/request'

export default {
  /* 是否需要权限访问系统,如果不需要权限,那么下面的接口都是不通的 */
  needSecurity() { return request.get('/needSecurity') },

  /* base apis */
  login(user) { return request.post('/login', user, headers.json) },
  logout() { return request.post('/logout') },

  /* admin apis */
  users() { return request.get('/security/admin/users') },
  roles() { return request.get('/security/admin/roles') },
  groupTree() { return request.get('/security/admin/group/tree') },
  resources() { return request.get('/security/admin/resources') },

  /* profile apis */
  username() { return request.get('/profile/username') },
  user() { return request.get('/profile/user') },
  profile() { return request.get('/profile') },
  accessGroups() { return request.get('/profile/accessGroups') },
  accessUsers() { return request.get('/profile/accessUsers') },
  accessRoles() { return request.get('/profile/accessRoles') },
  accessResources() { return request.get('/profile/accessResources') },
  menus() { return request.get('/profile/accessMenus') },
  changePassword(oldPassword, password) { return request.post('/profile/changePassword', { oldPassword, password }) },

  /* manage apis   */
  addUser(user) { return request.post('/security/user/add', user, headers.json) },
  delUser(username) { return request.post('/security/user/del', qs.stringify({ username }), headers.urlencoded) },
  userGrantRoles(username, roles) { return request.post(`/security/user/${username}/grantRoles`, qs.stringify({ username, roles }), headers.urlencoded) },
  userGrantGroups(username, groups) { return request.post(`/security/user/${username}/grantGroups`, qs.stringify({ username, groups }), headers.urlencoded) },
  resetPassword(username) { return request.post(`/security/user/${username}/resetPassword`) },
  queryUserAccessGroups(username) { return request.get(`/security/user/${username}/accessGroups`) },
  queryUserAccessUsers(username) { return request.get(`/security/user/${username}/accessUsers`) },
  queryUserAccessRoles(username) { return request.get(`/security/user/${username}/accessRoles`) },
  queryUserAccessResources(username) { return request.get(`/security/user/${username}/accessResources`) },

  addRole(role) { return request.post('/security/role/add', role, headers.json) },
  delRole(rolename) { return request.post('/security/role/del', qs.stringify({ rolename }), headers.urlencoded) },
  roleGrantResources(rolename, resources) { return request.post(`/security/role/${rolename}/grantResources`, qs.stringify({ rolename, resources }), headers.urlencoded) },
  roleGrantGroups(rolename, groups) { return request.post(`/security/role/${rolename}/grantGroups`, qs.stringify({ rolename, groups }), headers.urlencoded) },
  queryRoleAccessResources(rolename) { return request.get(`/security/role/${rolename}/accessResources`) },

  addGroup(parentGroup, childGroup) { return request.post('/security/group/add', qs.stringify({ parentGroup, childGroup }), headers.urlencoded) },
  delGroup(groupPath) { return request.post('/security/group/del', qs.stringify({ groupPath }), headers.urlencoded) },
  queryGroupMountUsers(groupPath, loadChild) { return request.get('/security/group/mount/users', { params: { groupPath, loadChild }}) },
  queryGroupMountRoles(groupPath, loadChild) { return request.get('/security/group/mount/roles', { params: { groupPath, loadChild }}) },
  queryGroupMountResources(groupPath, loadChild) { return request.get('/security/group/mount/resources', { params: { groupPath, loadChild }}) },
  queryMount(groupPath, loadChild) { return request.get('/security/group/mount', { params: { groupPath, loadChild }}) },

  queryUser(username) { return request.get(`/security/user/${username}`, { params: { username }}) },
  queryRole(rolename) { return request.get(`/security/role/${rolename}`, { params: { rolename }}) }
}
